Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix-ImproveCardDrawingLogic #42

Conversation

Abhishek-Punhani
Copy link

backend/engine.js

Fixed the card drawing logic which would draw cards from deck and move thrown cards back to deck once the deck is empty

Fixes: #8

}
}

shuffleDeck(deck) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already defined in deck.ts. We'd reuse that here.

@Abhishek-Punhani Abhishek-Punhani force-pushed the FixIssue#8 branch 2 times, most recently from 7ebc181 to 771b615 Compare June 1, 2024 17:27
}
// Move thrown cards back to the deck
this.cardDeck = this.thrownCards;
this.thrownCards = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have at least one thrown card since for progressing the game.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it checks throwncard lenght before throwing
if (this.cardDeck.length === 0) {
if (this.thrownCards.length === 0) {
throw new Error('No cards left to draw');
}
// Move thrown cards back to the deck
this.cardDeck = this.thrownCards;
this.thrownCards = [];

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean we shouldn't put all the thrown cards back to the deck. We reserve one card there, so that the game can progress (since the next move depends on the last card thrown)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh srry lemme update and add last thrown card also

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be a bit more formal.(●'◡'●)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okk...

@Abhishek-Punhani Abhishek-Punhani force-pushed the FixIssue#8 branch 2 times, most recently from 9411f44 to 42825e3 Compare June 1, 2024 18:22

// Move thrown cards back to the deck and updated last thrown card
this.cardDeck = this.thrownCards.slice(0, -1);
this.lastThrownCard = this.thrownCards[this.thrownCards.length - 1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, lastThrownCard is redundant as we can always just take the last element of the thrownCards array.
I think it would be nice to convert lastThrownCard into a function, which just returns the last element of thrownCards, or null if it is empty.

And here, you can use Array.splice to remove all but the last element of thrownCards and transfer it to cardDeck.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, break the excessively long line in the commit message into multiple lines.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I added updateLastThrownCard function , kindly review the changes

@@ -1,6 +1,7 @@
import { getShuffledCardDeck } from './deck';
import { handleEvent } from './gameEvents';

import { shuffle } from './deck';
import { isUndefined } from 'util';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to remove this , earlier i was thinking to use this function but then i removed it

Fixed the card drawing logic which would also manage moving thrown cards to deck

Before drawing the card we check that that deck has sufficient card , if not we move thrown cards back to the deck

 otherwise we draw a card and push into the cards of the current player

Fixes:shivansh-bhatnagar18#8
@kuv2707
Copy link
Collaborator

kuv2707 commented Jun 2, 2024

Great work here!
It would be great if you could add some tests for the logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve card drawing logic
2 participants